#ifndef RP2TGA_H
#define RP2TGA_H

#include "Rp2System.h"
#include "Rp2Image.h"

namespace Rp2
{

class Renderer;

class Tga : public Image
{
public:
	Tga();
	~Tga();

	// member access
	inline unsigned char* GetData();
	inline unsigned short GetWidth();
	inline unsigned short GetHeight();
	inline char GetBitsPerPixel();
	inline int GetComponent();
	inline unsigned int GetFormat();

	// load .tga flie as texture
	static bool LoadTGATexture(const char* acFileName, unsigned int uiMinFilter,
		unsigned int uiMagFilter, unsigned int uiWrapMode);
	// load .tga file as rectangle texture
	static bool LoadTGATextureRect(const char* acFileName, unsigned int uiMinFilter,
		unsigned int uiMagFilter, unsigned int uiWrapMode);

	// print current screen to .tga file.
	// NOTE: call swapbuffer before this function
	static bool ScreenToTGA(const char* acFileName, Renderer* pkRenderer);
	// read a .tga file from desk
	static bool ReadTGA(const char* acFileName, Tga* pkTga);
	
	// load a .tga file for Image
	static Image* LoadTGA(const unsigned char* aucBuffer, const std::string& rkFilename);

	// set the pack type to 1
	#pragma pack(push) 
	#pragma  pack(1)
	typedef struct
	{
		char cIDSize;	// Size of ID field that follows header (0)
		char cColorMapType; // 0 = None, 1 = paletted.
		char cImageType;	// 0 = None, 1 = indexed, 2 = rgb, 3 = grey, +8 = rle.
		unsigned short usColorMapStart; // First color map entry
		unsigned short usColorMapLength; // Number of colors
		unsigned char  ucColorMapBits; //bits per palette entry
		unsigned short usXStart; // image x origin
		unsigned short usYStart; // image y origin
		unsigned short usWidth;  // width in pixels
		unsigned short usHeight; // height in pixels
		char cBits;				 // bits per pixel (8, 16, 24, 32)
		char cDescriptor;		 // image descriptor
	}TGAHeader;
	// restore pack type
	#pragma pack(pop) 

private:
	TGAHeader m_kTgaHeader;					// TGA file header
	unsigned long m_ulImageSize;			// Size in bytes of image
	unsigned int m_uiChannel;				// Data channels. 1,3,4
	unsigned int m_eFormat;					// Format type for opengl
	int m_iComponents;
	unsigned char* m_acBits;							// Pointer to image data	
	int m_aiViewport[4];					// Viewport in pixels
	unsigned int m_uiLastBuffer;			// Storage for the current read buffer setting
};
#include "Rp2Tga.inl"

}

#endif